<?php
/**
 * Created by PhpStorm.
 * User: yuyiwen
 * Date: 2018/11/21
 * Time: 下午2:12
 */

namespace app\admin\controller;

use think\Request;
class User extends Common
{
    public function index ()
    {
        // 判断用户是否有搜索
        $search = input('get.search' , '' , 'htmlspecialchars');
        $ad = db('admin');
        $config = [];
        if ($search != '')
        {
            $config = ['query'=>['search' =>$search ]];

            $ad
                ->whereLike('username' , "%{$search}%" , 'or')
                ->whereLike('nickname' , "%{$search}%",'or');
        }

        $ad_list =$ad->paginate(10 , false , $config);
        $page  = $ad_list->render();
        $search = ['pagination' , 'disabled' , 'active'];
        $replace = ['am-pagination tpl-pagination' , 'am-disabled' , 'am-active'];
        $page = str_replace($search , $replace , $page);

        $request = Request::instance();
        if ($request -> isAjax()) {
            return json(['ad_list' => $ad_list , 'page' => $page]);
        }else {
            $this->assign('page', $page);
            $this->assign('ad_list', $ad_list);
            return view();
        }
    }

    public function addUser ()
    {
        $post = input('post.');
        $post['password'] = md5($post['password']);
        $id = db('admin')->insertGetId($post);
        db('adminRole')->insert(['admin_id' => $id , 'role_id' => 2]);
    }
}